import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Location } from '@angular/common';

import { aroAnimation } from '../../../../animation';
import { AccountService } from '../../../../services/account.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  animations: [aroAnimation]
})
export class LoginComponent implements OnInit {

  public readerId: string = '';

  public msg: string = '';
  public timer: any;
  public state: boolean = false;

  constructor(private router: Router, private currentRouter: ActivatedRoute, private location: Location,
    private accountService: AccountService
  ) { }

  ngOnInit() {
    // this.accountService.login()
  }

  public goBack(): void {
    this.location.back();
  }

  public loginBtn_onclick(phoneNumber, password): void {
    // console.log(phoneNumber, password);
    this.accountService.login({ phone: phoneNumber, password: password }).subscribe(res => {
      console.log(res);
      if (res.Code == 100) {
        // console.log(res);
        this.readerId = res.Data.Id;
        localStorage.setItem('ReaderId', this.readerId);
        this.router.navigate(['./content/home']);
      } else if (res.Code == 102) {
        this.msg = '手机号码或密码不正确';
        this.state = true;
        this.timer = setInterval(() => {
          this.state = false;
        }, 2000);
      } else if (res.Code == 110) {
        this.msg = '用户信息不存在';
        this.state = true;
        this.timer = setInterval(() => {
          this.state = false;
        }, 1000);
      }

    });
  }



}
